package za.ac.cput.arrowgame.service.movearrow;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import org.springframework.stereotype.Service;
import za.ac.cput.arrowgame.app.exceptions.InvalidInput;
import za.ac.cput.arrowgame.app.exceptions.Win;
import za.ac.cput.arrowgame.model.Arrow;
import za.ac.cput.arrowgame.model.Direction;
import za.ac.cput.arrowgame.model.Grid;
import za.ac.cput.arrowgame.model.GridLocation;

@Service("moveArrowService")
public class MoveArrowServiceImpl implements MoveArrowService {

    public Grid moveArrowForward(Grid gridModel) throws InvalidInput, Win {
        ImageIcon nextPosition = null;
        Arrow arrow = gridModel.getArrow();
        Arrow cloneArrow = new Arrow();
        try {
            cloneArrow = (Arrow) arrow.clone();
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(MoveArrowServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        Direction direction = arrow.getDirection();
        GridLocation gridLocation = arrow.getGridLocation();
        switch (direction) {
            case NORTH:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate() - 1, gridLocation.getxCoordinate());
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate() - 1, gridLocation.getxCoordinate()));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
            case NORTHEAST:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate() - 1, gridLocation.getxCoordinate() + 1);
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate() - 1, gridLocation.getxCoordinate() + 1));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
            case EAST:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate(), gridLocation.getxCoordinate() + 1);
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate(), gridLocation.getxCoordinate() + 1));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
            case SOUTHEAST:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate() + 1, gridLocation.getxCoordinate() + 1);
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate() + 1, gridLocation.getxCoordinate() + 1));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
            case SOUTH:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate() + 1, gridLocation.getxCoordinate());
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate() + 1, gridLocation.getxCoordinate()));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
            case SOUTHWEST:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate() + 1, gridLocation.getxCoordinate() - 1);
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate() + 1, gridLocation.getxCoordinate() - 1));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
            case WEST:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate(), gridLocation.getxCoordinate() - 1);
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate(), gridLocation.getxCoordinate() - 1));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
            case NORTHWEST:
                nextPosition = (ImageIcon) gridModel.getValueAt(gridLocation.getyCoordinate() - 1, gridLocation.getxCoordinate() - 1);
                cloneArrow.setGridLocation(new GridLocation(gridLocation.getyCoordinate() - 1, gridLocation.getxCoordinate() - 1));
                if (nextPosition.getDescription().contains("invalid")) {
                    throw new InvalidInput("Invalid Move");
                } else if (nextPosition.getDescription().contains("end")) {
                    throw new Win("You Won");
                }
                break;
        }
//        Boolean validMove = Facade.getInstance().getValidateMove().isValidMove(gridModel, cloneArrow);
        gridModel.setArrow(cloneArrow);
        return gridModel;
    }
}
